﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SM2TestRunner;

namespace Popups
{
    public partial class DashboardInputs : Form
    {
        public static string strAction;

        public DashboardInputs()
        {
            InitializeComponent();
        }

        public static DialogResult ShowDashboardCreationInputs(string action)
        {
            DashboardInputs dialog = new DashboardInputs();
            strAction = action;
            return dialog.ShowDialog();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            InputDispatcher.TEST_PARAMETER = new TestParameters();

            switch (strAction)
            {
                case "createDashBoard":
                    #region
                    labelTitle.Text = "Creation Dashboard";
                    string strDBName_Creation;
                    if (textBoxDBName.Enabled)
                    {
                        strDBName_Creation = textBoxDBName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strDBName_Creation);
                    }
                    else
                    {
                        strDBName_Creation = cbbDashboard.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strDBName_Creation);
                    }

                    //Update new Dashboard
                    Repository.ServerHardDataUtil.saveDashboardName(strDBName_Creation);

                    if (cbAutoArrange.Checked)
                    {
                        string strAutoRange = cbAutoArrange.Checked.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Auto Arrange", strAutoRange.ToLower());
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Auto Arrange", "false");
                    }

                    if (cbShare.Checked)
                    {
                        string strShare = cbShare.Checked.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Share", strShare.ToLower());
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Share", "false");
                    }

                    string strChartName_Creation = comboBoxCHART.SelectedValue.ToString();
                    InputDispatcher.TEST_PARAMETER.add("Chart Name", strChartName_Creation);
                    string strProfileName_Creation;
                    if (tbProfilename.Enabled)
                    {
                        strProfileName_Creation = tbProfilename.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("Profile Name", strProfileName_Creation);
                    }
                    else
                    {
                        strProfileName_Creation = comboBoxPROFILE.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Profile Name", strProfileName_Creation);
                    }

                    //Save panel name into file
                    string strPanelName_Creation = strChartName_Creation + " (" + strProfileName_Creation + ")";
                    Repository.TestDataUtil.addPanel2PanelFile(strPanelName_Creation, strDBName_Creation);

                    break;
                    #endregion

                case "removeDashBoard":
                    #region
                    labelTitle.Text = "Removal Dashboard";
                    string strRemoveDBName;
                    if (textBoxDBName.Enabled)
                    {
                        strRemoveDBName = textBoxDBName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strRemoveDBName);
                    }
                    else
                    {
                        strRemoveDBName = cbbDashboard.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strRemoveDBName);
                    }

                    //Remove
                    Repository.ServerHardDataUtil.removeDashboardName(strRemoveDBName);

                    //Delete Panel File
                    Repository.TestDataUtil.deletePanelFile(strRemoveDBName);

                    break;
                    #endregion

                case "editDashBoard":
                    #region
                    labelTitle.Text = "Edit Dashboard";
                    string strSelectedDBName_EditDB;
                    if (textBoxDBName.Enabled)
                    {
                        strSelectedDBName_EditDB = textBoxDBName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strSelectedDBName_EditDB);
                    }
                    else
                    {
                        strSelectedDBName_EditDB = cbbDashboard.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strSelectedDBName_EditDB);
                    }

                    //Remove & update
                    string strNewDashboardName = "";
                    if (tbNewDashboardName.Enabled)
                    {
                        strNewDashboardName = tbNewDashboardName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("New DashBoard Name", strNewDashboardName);

                        //Remove
                        Repository.ServerHardDataUtil.removeDashboardName(strSelectedDBName_EditDB);

                        //Update new Dashboard
                        Repository.ServerHardDataUtil.saveDashboardName(strNewDashboardName);
                    }

                    if (cbAutoArrange.Checked)
                    {
                        string strAutoRange = cbAutoArrange.Checked.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Auto Arrange", strAutoRange.ToLower());
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Auto Arrange", "false");
                    }

                    if (cbShare.Checked)
                    {
                        string strShare = cbShare.Checked.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Share", strShare.ToLower());
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Share", "false");
                    }

                    //Add more panel into Panel File
                    Repository.TestDataUtil.updatePanelFile(strSelectedDBName_EditDB, strNewDashboardName);

                    break;
                    #endregion

                case "addPanelToDashboard":
                    #region
                    labelTitle.Text = "Adding Panel into Dashboard";
                    string strDBName_AddPanel;
                    if (textBoxDBName.Enabled)
                    {
                        strDBName_AddPanel = textBoxDBName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strDBName_AddPanel);
                    }
                    else
                    {
                        strDBName_AddPanel = cbbDashboard.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("DashBoard Name", strDBName_AddPanel);
                    }

                    string strChartName_AddPanel = comboBoxCHART.SelectedValue.ToString();
                    InputDispatcher.TEST_PARAMETER.add("Chart Name", strChartName_AddPanel);

                    string strProfileName_AddPanel;
                    if (tbProfilename.Enabled)
                    {
                        strProfileName_AddPanel = tbProfilename.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("Profile Name", strProfileName_AddPanel);
                    }
                    else
                    {
                        strProfileName_AddPanel = comboBoxPROFILE.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Profile Name", strProfileName_AddPanel);
                    }

                    //Add more panel into Panel File
                    string strPanelName_AddPanel = strChartName_AddPanel + " (" + strProfileName_AddPanel + ")";
                    Repository.TestDataUtil.addPanel2PanelFile(strPanelName_AddPanel, strDBName_AddPanel);

                    break;
                    #endregion

                default:
                    break;
            }

            this.Close();
        }

        private void DashboardCreationInputs_Load(object sender, EventArgs e)
        {
            //Feed data

            #region DASHBOARD
            cbbDashboard.DisplayMember = "Attribute2";
            cbbDashboard.ValueMember = "Attribute1";
            try
            {
                cbbDashboard.DataSource = Repository.ServerHardDataUtil.buildDashboardsDataTable();
            }
            catch (Exception ex)
            {
                Utilities.GlobalObject.LOGGER.log("Failed to load ListBox Dashboard.CHART Utilities.GlobalInit.buildChartTypeDataTable()");
                Utilities.GlobalObject.LOGGER.log(ex.ToString());
            }
            #endregion

            #region CHART
            comboBoxCHART.DisplayMember = "Attribute2";
            comboBoxCHART.ValueMember = "Attribute1";
            try
            {
                comboBoxCHART.DataSource = Repository.ServerHardDataUtil.buildChartsDataTable();
            }
            catch (Exception ex)
            {
                Utilities.GlobalObject.LOGGER.log("Failed to load ListBox Dashboard.CHART Utilities.GlobalInit.buildChartsDataTable()");
                Utilities.GlobalObject.LOGGER.log(ex.ToString());
            }
            #endregion

            #region PROFILE
            comboBoxPROFILE.DisplayMember = "Attribute2";
            comboBoxPROFILE.ValueMember = "Attribute1";
            try
            {
                comboBoxPROFILE.DataSource = Repository.ServerHardDataUtil.buildProfilesDataTable();
            }
            catch (Exception ex)
            {
                Utilities.GlobalObject.LOGGER.log("Failed to load ListBox Dashboard.CHART Utilities.GlobalInit.buildProfilesDataTable()");
                Utilities.GlobalObject.LOGGER.log(ex.ToString());
            }
            #endregion

            //GUI

            switch (strAction)
            {
                case "createDashBoard":
                    #region
                    labelTitle.Text = "Creation Dashboard";
                    cbbDashboard.Enabled = false; radioButton3.Checked = false;
                    textBoxDBName.Enabled = true; radioButton4.Checked = true;
                    cbAutoArrange.Enabled = true;cbAutoArrange.Checked = true;
                    cbShare.Enabled = true;cbShare.Checked = false;

                    //Disable label & textfiel of New DB Name
                    labelNewDBName.Enabled = false;
                    tbNewDashboardName.Enabled = false;

                    comboBoxCHART.Enabled = true;

                    comboBoxPROFILE.Enabled = true;radioButton2.Checked = true;
                    tbProfilename.Enabled = false;radioButton1.Checked = false;
                    break;
                    #endregion

                case "removeDashBoard":
                    #region
                    labelTitle.Text = "Removal Dashboard";
                    textBoxDBName.Enabled = false;radioButton4.Checked = false;
                    cbbDashboard.Enabled = true;radioButton3.Checked = true;
                    cbAutoArrange.Enabled = false;cbAutoArrange.Checked = true;
                    cbShare.Enabled = false;cbShare.Checked = false;

                    //Disable label & textfiel of New DB Name
                    labelNewDBName.Enabled = false;
                    tbNewDashboardName.Enabled = false;

                    comboBoxCHART.Enabled = false;

                    groupBoxProfile.Enabled = false;
                    break;
                    #endregion

                case "editDashBoard":
                    #region
                    labelTitle.Text = "Editing Dashboard";
                    textBoxDBName.Enabled = false;radioButton4.Checked = false;
                    cbbDashboard.Enabled = true;radioButton3.Checked = true;
                    cbAutoArrange.Enabled = true;cbAutoArrange.Checked = true;
                    cbShare.Enabled = true;cbShare.Checked = false;

                    comboBoxCHART.Enabled = false;

                    groupBoxProfile.Enabled = false;
                    break;
                    #endregion

                case "addPanelToDashboard":
                    #region
                    labelTitle.Text = "Adding Panel into Dashboard";
                    textBoxDBName.Enabled = false; radioButton4.Checked = false;
                    cbbDashboard.Enabled = true; radioButton3.Checked = true;
                    cbAutoArrange.Enabled = false;cbAutoArrange.Checked = true;
                    cbShare.Enabled = false;cbShare.Checked = false;

                    //Disable label & textfiel of New DB Name
                    labelNewDBName.Enabled = false;
                    tbNewDashboardName.Enabled = false;

                    comboBoxCHART.Enabled = true;

                    groupBoxProfile.Enabled = true;
                    comboBoxPROFILE.Enabled = true; radioButton2.Checked = true;
                    tbProfilename.Enabled = false; radioButton1.Checked = false;
                    break;
                    #endregion

                default:
                    break;
            }
        }

        private void btnDISCARD_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxPROFILE.Enabled = false;
            tbProfilename.Enabled = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            tbProfilename.Enabled = false;
            comboBoxPROFILE.Enabled = true;
        }

        private void tbProfilename_TextChanged(object sender, EventArgs e)
        {
            comboBoxPROFILE.Enabled = false;
            tbProfilename.Enabled = true;
        }

        private void comboBoxPROFILE_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbProfilename.Enabled = false;
            comboBoxPROFILE.Enabled = true;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            cbbDashboard.Enabled = false;
            textBoxDBName.Enabled = true;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            cbbDashboard.Enabled = true;
            textBoxDBName.Enabled = false;
        }
    }
}
